package HashTable;

public class _36_ValidSudoku {
    char[][] board;
    public boolean isValidSudoku(char[][] board) {
        this.board = board;
        for (int i = 0; i < 9; i++) {
            for (int j = 0; j < 9; j++) {
                if (board[i][j] != '.') {
                    if (!check(i, j, board[i][j])) {
                        return false;
                    }
                }
            }
        }
        return true;
    }

    public boolean check(int row, int col, char c) {
        for (int i = 0; i < 9; i++) {
            if (col != i && board[row][i] != '.' && board[row][i] == c) {
                return false;
            }
            if (row != i &&board[i][col] != '.' && board[i][col] == c) {
                return false;
            }
            if ((3 * (row / 3) + i / 3) != row && (3 * (col / 3) + i % 3) != col && board[3 * (row / 3) + i / 3][3 * (col / 3) + i % 3] != '.' && board[3 * (row / 3) + i / 3][3 * (col / 3) + i % 3] == c) {
                return false;
            }
        }
        return true;
    }
}
